<?php
/**
 * Created by PhpStorm.
 * User: reshared
 * Date: 2017/9/29
 * Time: 上午10:37
 */

namespace Index\Repository;


use Extend\SmsSingleSender;
use Index\Model\SmsCodeModel;
use Index\Repository\Tra\InstanceTrait;

class SMSRepository extends BaseRepository
{
    use InstanceTrait;

    /**
     * 发送忘记密码的验证码
     * @param $username
     * @return bool
     */
    public function sendForgetCode($username)
    {
        if (isEmail($username)){
            return $this->sendEmailCode($username);
        } elseif(isMobile($username)) {
            return $this->sendMobileCode($username, SmsCodeModel::FORGET_MOBILE);
        }

        return false;
    }

    /**
     * 发送注册的验证码
     * @param $mobile
     * @return bool
     */
    public function sendRegisterCode($mobile)
    {
        $code = randToken(0);
        $tplId = C('QCLOUD_SMS_REGISTER_TEMPLID');
        if (!$this->send($mobile, $code, $tplId)) {
            return false;
        }
        $time = time();
        $data = array(
            'mobile'=>$mobile,
            'code'=>$code,
            'send_time'=>$time,
            'code_type' => 0,
            'sms_expire_time' => $time + 3600
        );
        return (bool)$this->model('smsCode')->add($data);
    }

    /**
     * 发送邮箱验证码
     * @param $email
     * @return bool
     */
    public function sendEmailCode($email)
    {
        if (!isEmail($email)) {
            return false;
        }
        $code = randCode();
        $time = time();
        $data = array(
            'mobile'=>$email,
            'code'=>$code,
            'send_time'=>$time,
            'code_type' => 2,
            'sms_expire_time' => $time + 3600
        );
        return (bool)$this->model('smsCode')->add($data);
    }

    /**
     * 发送手机验证码
     * @param $mobile
     * @param $type
     * @return bool
     */
    public function sendMobileCode($mobile, $type)
    {
        if ($type != 0 && $type != 1) {
            return false;
        }
        $code = randToken(0);
        $tplId = C('QCLOUD_SMS_FORGETPW_TEMPLID');
        if (!$this->send($mobile, $code, $tplId)) {
            return false;
        }
        $time = time();
        $data = array(
            'mobile'=>$mobile,
            'code'=>$code,
            'send_time'=>$time,
            'code_type' => $type,
            'sms_expire_time' => $time + 3600
        );
        return (bool)$this->model('smsCode')->add($data);
    }

    /**
     * 验证验证码正确性
     * @param $code
     * @param $mobile
     * @param $type
     * @return mixed
     */
    public function validateCode($code, $mobile, $type)
    {
        return $this->model('smsCode')->validate($code, $mobile, $type);
    }

    /**
     * 发送
     * @param $mobile
     * @param $code
     * @param $tplId
     * @return bool
     */
    protected function send($mobile, $code, $tplId)
    {
        //若为设置为测试,咋不实发短信,直接返回结果
        if (defined(APP_DEBUG)) {
            return true;
        }

        $singleSender = new SmsSingleSender(C('QCLOUD_SMS_APPID'), C('QCLOUD_SMS_APPKEY'));
        $result = $singleSender->sendWithParam("86", $mobile, $tplId, array($code, '60'), '', '', '');
        $result = json_decode($result, true);
        if ($result['result'] != 0) {
            return false;
        }
        return true;
    }
}